package com.ruoyi.zhmk.service.impl;

import java.util.List;

import com.ruoyi.carApi.click.CarApiClick;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.zhmk.utils.CarApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhmk.mapper.ZhmkCarMapper;
import com.ruoyi.zhmk.domain.ZhmkCar;
import com.ruoyi.zhmk.service.IZhmkCarService;
import com.ruoyi.common.core.text.Convert;

/**
 * 车牌设备Service业务层处理
 * 
 * @author sxl
 * @date 2021-06-15
 */
@Service
public class ZhmkCarServiceImpl implements IZhmkCarService 
{
    @Autowired
    private ZhmkCarMapper zhmkCarMapper;

    /**
     * 查询车牌设备
     * 
     * @param carId 车牌设备ID
     * @return 车牌设备
     */
    @Override
    public ZhmkCar selectZhmkCarById(Long carId)
    {
        return zhmkCarMapper.selectZhmkCarById(carId);
    }

    /**
     * 查询车牌设备列表
     * 
     * @param zhmkCar 车牌设备
     * @return 车牌设备
     */
    @Override
    public List<ZhmkCar> selectZhmkCarList(ZhmkCar zhmkCar)
    {
        return zhmkCarMapper.selectZhmkCarList(zhmkCar);
    }

    /**
     * 新增车牌设备
     * 
     * @param zhmkCar 车牌设备
     * @return 结果
     */
    @Override
    public int insertZhmkCar(ZhmkCar zhmkCar)
    {
        String carIp = zhmkCar.getCarIp();
        zhmkCar.setCreateTime(DateUtils.getNowDate());
        //创建连接设备
        CarApiClick carApiClick = CarApiUtil.getCarApiClick();
        String[] ips = {carIp};
        carApiClick.quickConnect(ips);

        return zhmkCarMapper.insertZhmkCar(zhmkCar);
    }

    /**
     * 修改车牌设备
     * 
     * @param zhmkCar 车牌设备
     * @return 结果
     */
    @Override
    public int updateZhmkCar(ZhmkCar zhmkCar)
    {
        String carIp = zhmkCar.getCarIp();
        zhmkCar.setUpdateTime(DateUtils.getNowDate());
        //重新连接设备
        //创建连接设备
        CarApiClick carApiClick = CarApiUtil.getCarApiClick();
        String[] ips = {carIp};
        carApiClick.quickConnect(ips);
        return zhmkCarMapper.updateZhmkCar(zhmkCar);
    }

    /**
     * 删除车牌设备对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarByIds(String ids)
    {
        String[] strings = Convert.toStrArray(ids);
        for (String string : strings) {
            //查询出ip
            ZhmkCar zhmkCar = this.selectZhmkCarById(Long.parseLong(string));
            String carIp = zhmkCar.getCarIp();
            //断开设备
            CarApiClick carApiClick = CarApiUtil.getCarApiClick();
            carApiClick.closeConnect(carIp);
        }

        return zhmkCarMapper.deleteZhmkCarByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除车牌设备信息
     * 
     * @param carId 车牌设备ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarById(Long carId)
    {
        //查询出ip
        ZhmkCar zhmkCar = this.selectZhmkCarById(carId);
        String carIp = zhmkCar.getCarIp();
        //断开设备
        CarApiClick carApiClick = CarApiUtil.getCarApiClick();
        carApiClick.closeConnect(carIp);
        return zhmkCarMapper.deleteZhmkCarById(carId);
    }
}
